package dao.impl;

import bean.Student;
import dao.StudentDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public List<Student> queryAll()
    {
        List<Student> studentList = jdbcTemplate.query("select * from student", new BeanPropertyRowMapper<Student>(Student.class));
        return studentList;
    }
    public void add(Student student)
    {
        jdbcTemplate.update("insert into student value(?,?,?)",student.getId(),student.getName(),student.getAge());
    }
    public void delete(int id)
    {
        jdbcTemplate.update("delete from student where id=?",id);
    }
    public Student queryOne(int id)
    {
        Student student=jdbcTemplate.queryForObject("select * from student where id=?",new BeanPropertyRowMapper<Student>(Student.class),id);
        return student;
    }
}
